package com.fr.ticket;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月16日 下午3:39:58
 */
public class LvmamaTicketDetail {
	private static final String simple_file = "E:\\ticket\\lvmama_simple.txt";
	private static final String detail_file = "E:\\ticket\\lvmama_detail.txt";
//	private static final String goods_id_file = "E:\\ticket\\lvmama_goods_id.txt";
	private static final String tab = "\t";
	private static final String line = "\n";
	
	public static void main(String[] args) throws IOException {
		TransportClient client = ESUtils.getClient();
		SearchRequestBuilder builder = client.prepareSearch("product").setTypes("product").setSize(1000);
		builder.setScroll(new TimeValue(10000000));
		builder.addSort(SortBuilders.fieldSort("productId").order(SortOrder.DESC));
		String string = LvmamaDataUtil.getTicketProductScroll(builder.toString(), null);
		FileWriter simpleWriter = new FileWriter(simple_file);
		FileWriter detailWriter = new FileWriter(detail_file);
//		FileWriter goodsWriter = new FileWriter(goods_id_file);
		Map<String, Long> countgoodsMap = new HashMap<>();
		while (true && string != null) {
			JSONObject object = JSON.parseObject(string);
			String scrollId = object.getString("_scroll_id");
			List<JSONObject> ticketProduct = (List<JSONObject>) ((JSONObject) object.get("hits")).get("hits");
			if(CollectionUtils.isNotEmpty(ticketProduct)){
				for (JSONObject hit : ticketProduct) {
					JSONObject source = hit.getJSONObject("_source");
					if(13 == source.getInteger("categoryId") || "Y".equalsIgnoreCase(source.getString("abroad"))){
						continue;
					}
					StringBuffer simple = new StringBuffer();
					
					String id = source.get("productId").toString();
					String name = source.get("productName").toString();
					name = name.replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
					String provinceName = String.valueOf(source.get("provinceName"));
					String address = String.valueOf(source.get("address"));
					address = address.replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
					String price = source.get("sellPrice").toString();
					String star = String.valueOf(source.get("star"));
					simple.append(provinceName).append(tab).append(id).append(tab).append(name).append(tab).append(address).append(tab).append(price).append(tab).append(star);
					simpleWriter.append(simple).append(line);
					try {
						double p = Double.parseDouble(price);
						if (p > 0) {
							SearchRequestBuilder goodsSearchBuilder = client.prepareSearch("goods").setTypes("goods").setSize(1000);
							goodsSearchBuilder.setQuery(QueryBuilders.termQuery("productId", id));
							String goodsString = LvmamaDataUtil.getTicketProductOrGoods(goodsSearchBuilder.toString(), 2);
							JSONObject goodsObject = JSON.parseObject(goodsString);
							List<JSONObject> ticketGoods = (List<JSONObject>) ((JSONObject) goodsObject.get("hits")).get("hits");
							if(CollectionUtils.isNotEmpty(ticketGoods)){
								
								boolean first = true;
								for (JSONObject sh : ticketGoods) {
									StringBuffer detail = new StringBuffer();
									if (first) {
										detail.append(simple).append(tab);
										first = false;
									} else {
										detail.append(tab).append(tab).append(tab).append(tab).append(tab).append(tab);
									}
									JSONObject goodsSource = sh.getJSONObject("_source");
									String suppGoodsId = String.valueOf(goodsSource.get("suppGoodsId"));
									String goodsName = String.valueOf(goodsSource.get("goodsName"));
									goodsName = goodsName.replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
									String goodsprice = String.valueOf(goodsSource.get("sellPrice"));
									detail.append(suppGoodsId).append(tab).append(goodsName).append(tab).append(goodsprice).append(tab);
									String[] descs = LvmamaGoodsDesc.map.get(suppGoodsId);
									if(descs != null){
										detail.append(descs[1]).append(tab).append(descs[2]).append(tab).append(descs[3]).append(tab).append(descs[4]).append(tab);
									}
									detailWriter.append(detail).append(line);
//									goodsWriter.append(suppGoodsId).append(",");
								}
								
							}
							Long goodsCount = countgoodsMap.get(provinceName);
							if(goodsCount == null){
								goodsCount = 0L;
							}
							goodsCount += ((JSONObject) goodsObject.get("hits")).getLong("total");
							countgoodsMap.put(provinceName, goodsCount);
						}else{
							detailWriter.append(simple).append(line);
						}
					} catch (Exception e) {
						System.out.println(JSON.toJSON(e));
					}
				}
				string = LvmamaDataUtil.getTicketProductScroll("{}", scrollId);
			}else{
				break;
			}
		}
		detailWriter.close();
		simpleWriter.close();
//		goodsWriter.close();
		System.out.println(countgoodsMap);
	}

}
